package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.AuthenticationCenterContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.AuthenticationCenterPresenter;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.io.File;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * @author NineTailDemonFox
 * @date 2019/7/24 9:34
 * 功能描述：认证中心
 * 联系方式：1037438704@qq.com
 */
public class AuthenticationCenterActivity extends BaseActivity implements AuthenticationCenterContract {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.text_video)
    TextView textVideo;
    @BindView(R.id.fl_gone_1)
    FrameLayout fl_gone_1;
    @BindView(R.id.fl_gone_2)
    FrameLayout fl_gone_2;
    @BindView(R.id.videoplayer)
    JZVideoPlayerStandard videoplayer;
    @BindView(R.id.text_verify_code)
    TextView text_verify_code;
    @BindView(R.id.webViewOne)
    TextView webViewOne;
    @BindView(R.id.webViewTwo)
    TextView webViewTwo;


    private AuthenticationCenterPresenter presenter;
    private String authentication = "";
    public long duration = -1;
    private String path, have = "2";

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_authentication_center;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        textCenter.setText("认证中心");
        imageleftFinish.setVisibility(View.VISIBLE);
        presenter = new AuthenticationCenterPresenter(this, this);
        presenter.center(token);
    }

    @OnClick({R.id.image_left_finish, R.id.text_video})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            case R.id.text_video:
//                ToastUtil.show("获取到的数据" + authentication);
//                 状态  1待审核 2审核通过 0未认证 3审核失败
//                getIdentification = Preferences.getInstance().getString(this, "user", "identification");
                if (TextUtils.isEmpty(authentication)) {
                    return;
                }
                if (authentication.equals("3") || authentication.equals("0")) {
                    // 录制
                    if (have.equals("2")) {
                        ToastUtil.show("最少在相册上传一张照片才能认证");
                        return;
                    }
                    if (path == null) {
                        startActivityForResult(new Intent(this, CameraActivity.class), 10);
                    } else {
                        File file = new File(path);
                        HttpRequest.POST(me, "Authentication/editVideo"
                                , new Parameter()
                                        .add("token", token)
                                        .add("video", file)
                                , new ResponseListener() {
                                    @Override
                                    public void onResponse(String response, Exception error) {
                                        if (error == null) {
                                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                            if (map.get("code").equals("1")) {
                                                MessageDialog.show(me,
                                                        "提示", map.get("message"),
                                                        "知道了").setOkButton(new OnDialogButtonClickListener() {
                                                    @Override
                                                    public boolean onClick(BaseDialog baseDialog, View v) {
                                                        finish();
                                                        return false;
                                                    }
                                                });
                                            } else {
                                                ToastUtil.show(map.get("message"));
                                            }
                                        }
                                    }
                                });
                    }
                }
                break;
            default:
        }
    }

    //结果处理函数，当从secondActivity中返回时调用此函数
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10 && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            path = bundle.getString("path");
            Log.d("requestCode", "==============" + path);

            if (path == null) {
                fl_gone_1.setVisibility(View.VISIBLE);
                fl_gone_2.setVisibility(View.GONE);
            } else {
                fl_gone_2.setVisibility(View.VISIBLE);
                fl_gone_1.setVisibility(View.GONE);
                //获取的路径
//                MediaMetadataRetriever media = new MediaMetadataRetriever();
//                media.setDataSource(path);
//                Bitmap bitmap = media.getFrameAtTime();
                videoplayer.setUp(path,
                        JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,
                        "");
//                videoplayer.thumbImageView.setImageBitmap(bitmap);
            }
        }
    }


    @Override
    public void Success(Map<String, String> map) {
        authentication = map.get("status");
        have = map.get("have");
        getIdentification = map.get("status");
        text_verify_code.setText(map.get("verify_code"));
        webViewOne.setText(map.get("condition"));
        webViewTwo.setText(map.get("advantage"));
        // 状态 1待审核 2审核通过 3审核失败 0未认证
        if (authentication.equals("1")) {
            textVideo.setText("待审核");
            textVideo.setBackgroundResource(R.color.voip_interface_text_color);
            fl_gone_2.setVisibility(View.VISIBLE);
            fl_gone_1.setVisibility(View.GONE);
            videoplayer.setUp(map.get("video_path"),
                    JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,
                    "");
        } else if (authentication.equals("2")) {
            textVideo.setText("审核通过");
            videoplayer.setUp(map.get("video_path"),
                    JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,
                    "");
            textVideo.setBackgroundResource(R.color.voip_interface_text_color);
            fl_gone_2.setVisibility(View.VISIBLE);
            fl_gone_1.setVisibility(View.GONE);
        } else if (authentication.equals("3")) {
            textVideo.setText("审核失败");
            textVideo.setBackgroundResource(R.color.voip_interface_text_color);
            fl_gone_1.setVisibility(View.VISIBLE);
            fl_gone_2.setVisibility(View.GONE);
        } else {
            fl_gone_1.setVisibility(View.VISIBLE);
            fl_gone_2.setVisibility(View.GONE);
            textVideo.setText("上传认证视频");
        }



//        if (!TextUtils.isEmpty(map.get("video_path"))) {
//            fl_gone_2.setVisibility(View.VISIBLE);
//            fl_gone_1.setVisibility(View.GONE);
        //获取的路径
//            MediaMetadataRetriever media = new MediaMetadataRetriever();
//            media.setDataSource(map.get("video_path"));
//            Bitmap bitmap = media.getFrameAtTime();
//            videoplayer.thumbImageView.setImageBitmap(bitmap);
//        }
    }

    @Override
    public void Failed() {

    }

    @Override
    public void showLoading() {
        WaitDialog.show(me, "数据加载中");
    }


    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }
}